from tokenize import Token

import requests
from loguru import logger
from requests import session

from config import ENV

token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJpc3MiOiIxMjQuNzAuMzkuMjAxIiwiYXVkIjoiMTI0LjcwLjM5LjIwMSIsImlhdCI6MTc0NDYyMDI3NCwibmJmIjoxNzQ0NjIwMjc0LCJleHAiOjE3NDUyMjUwNzQsImp0aSI6IjFfYWRtaW4ifQ.pkbJuCK4tERkIJQs01SMsJoVXVSbRSGjD6HMQaUXJ44"

def request_hook(response,*args,**kwargs):
    logger.debug(f"响应结果:{response.text}")

class BaseRequest:
    def __init__(self):
        self._session=requests.session()
        self._session.headers.update({ "Content-Type": "application/json",
                "token": token})
        self._session.hooks["response"].append(request_hook)
        self._requests=self._session

    def _post(self,url,data):
        logger.debug(f"请求参数{url}{data}")
        return self._requests.post(f"{ENV.HOST}{url}",json=data).json()

    def _get(self,url,params):
        logger.debug(f"请求参数{url}{params}")
        return self._requests.get(f"{ENV.HOST}{url}",params=params).json()

    def _put(self, url, data):
        logger.debug(f"请求参数：{url}{data}")
        return self._requests.put(f"{ENV.HOST}{url}", json=data).json()

    def _delete(self, url, params=None):
        logger.debug(f"请求参数：{url}{params}")
        return self._requests.delete(f"{ENV.HOST}{url}", params=params).json()


# 需要登录的目的是得到token，暂时把token写成一个固定值
# token为全局变量
# class BaseLogin(BaseRequest):
#     def __init__(self):
#         data = {
#             "method": "get",
#             "url": "http://124.70.39.201/adminapi/login",
#             "params": {
#                 'username': 'admin',
#                 'password': 'Aa123456!',
#             }
#         }
#         r = requests.request(**data)
#         self.token = r.json()["data"]["token"]
#         print(self.token)
#     def login(self):
#         r=self._get("http://124.70.39.201/adminapi/login",{'username': 'admin','password': 'Aa123456!'})
#         print(r.json()["data"]["token"])
# if __name__ == '__main__':
#     BaseLogin().login()
